package com.ss;


import java.util.*;
import java.util.Date;
import java.sql.*;

import javax.swing.JOptionPane;

public class Dbconnection
{
/*
* Default constructor.
*/
public Dbconnection() {}
/*
* Method to establish connection to sqlserver using jdbc
*/
public Connection dbConnect(String db_connect_string,String db_userid, String db_password)
   {
       try
       {
        // Load Microsoft JDBC Driver 1.0
           Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
           
           Connection conn = DriverManager.getConnection(
           db_connect_string, db_userid, db_password);
           System.out.println("connected");
           return conn;
           
       }
       catch (Exception e)
       {
           e.printStackTrace();
           return null;
       }
   }

   
public void insertData(Connection conn, String uname, String pword)
   {
       Statement stmt;
       PreparedStatement ps;
       String a = uname;
       String b = pword;
       System.out.println("in 1st method");
       try
       {
       stmt = conn.createStatement();
       //ps = conn.prepareStatement("INSERT into Emp_login(Username,Password) VALUES(a,b)");
      /* ps = conn.prepareStatement("INSERT into Emp_login(Username,Password) VALUES(?,?)");
       ps.setString(1, a);
       ps.setString(2, b);
       ps.executeUpdate();*/
       //ps = (PreparedStatement) stmt.executeQuery("INSERT into Emp_login(Username,Password) VALUES(a,b)");
      String sql = ("INSERT INTO Emp_login VALUES ('" + a + "','" + b + ") ");
       stmt.close();
       }
       
       catch (Exception ex)
       {
       ex.printStackTrace();
       }
   }
   
public int Authenticate(Connection conn, String un, String ps)
   {
    Statement stmt;
    String name1 = un;
    String pass1 = ps;
    int i = 0;
       
    try
    {
    stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM Emp_login WHERE USERNAME='" + name1 +
    "'AND PASSWORD='"+pass1+"'");
    boolean rf = rs.next();
    if(rf)
    {
    System.out.println("found");
    i = 1;
    }
    else
    {
    System.out.println("not found");
    i = 0;
    }
       
    }
    catch( Exception e )
    {
    e.getMessage();
            
    }
    return i;
   
   }
public int chk_availability(Connection conn, String username){
String z = username;
Statement stmt1;
int i = 0;
       if(z.equals(""))
       {
        i=2;
        return i;
       }
       else
    {try
    {
   
    stmt1 = conn.createStatement();
    ResultSet rs = stmt1.executeQuery("SELECT * FROM Emp_login WHERE USERNAME='" + username+"'");
    boolean rf = rs.next();
    if(rf)
    {
    System.out.println("User name already exists");
    i = 0;
    }
    else
    {
    System.out.println("User name Valid");
    i = 1;
    }
       
    }
    catch( Exception e )
    {
    e.getMessage();
            
    }
return i;
    }
}

public void insert_newData(Connection conn, String First_Name, String mf_2,
String mf_3, String mf_4, String mf_6, String mf_7) {
// TODO Auto-generated method stub
}

public float getdata(Connection conn, String name, String date) {
Statement stmt;
try
{
stmt = conn.createStatement();
ResultSet stock_value = stmt.executeQuery("SELECT value FROM stocks WHERE stock='" + name +
    "'AND date='"+date+"'");
while (stock_value.next()) {
       // Get the data from the row using the column index
       String s = stock_value.getString(1);

       // Get the data from the row using the column name
       s = stock_value.getString("col_string");
       float x =Float.parseFloat( s );
       return x;
}
}
catch (Exception e )
    {
    e.getMessage();
    return(0);
}
// TODO Auto-generated method stub
return 0;
}
   
};
